package guoanmaker.operator.business.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import guoanmaker.operator.business.model.ResidueCount;
import guoanmaker.operator.business.modelnobind.PatentQueryRecordRo;
import guoanmaker.operator.business.repository.OperatorResidueCountRepository;

/**
 * 
 * @ClassName: PatentQueryRecordDao
 * @Description: 后台查询操作记录
 * @author xiaoqiang
 * @date 2017年12月16日 上午10:30:00
 *
 */
@Repository
public class PatentQueryRecordDao {
	@PersistenceContext
	EntityManager entityManager;
	@Autowired
	private OperatorResidueCountRepository repository;

	public Map<String, Object> findByParameter(int page, int pageSize, String title, String phone, String username,
			String beginDate, String endDate) {

		String jpql = "select new guoanmaker.operator.business.modelnobind.PatentQueryRecordRo(c.queryDate,c.keyWrod,c.title,c.kind,u.username,u.nickname) "
				+ "from PatentQueryRecord c LEFT JOIN c.user u where 1=1";
		if (title != null && !"".equals(title)) {
			jpql += " and c.title like '%" + title + "%'";
		}
		if (phone != null && !"".equals(phone)) {
			jpql += " and u.username like '%" + phone + "%'";
		}
		if (username != null && !"".equals(username)) {
			jpql += " and u.nickname like '%" + username + "%'";
		}
		if ((beginDate != null && !"".equals(beginDate)) && (endDate != null && !"".equals(endDate))) {

			String dates = beginDate; 
			String date1 = endDate;
			dates = dates.replace("Z", " UTC");//注意是空格+UTC
			date1 = date1.replace("Z", " UTC");//注意是空格+UTC
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//注意格式化的表达式
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
			Date date=null;
			Date date2 = null;
			String str="";
			String str1 = "";
			try {
				date = format.parse(dates);
				 str=sdf.format(date);  
				 date2 = format.parse(date1);
				 str1=sdf.format(date2); 
			} catch (ParseException e) {
				e.printStackTrace();
			}
			jpql += " and c.queryDate >='" + str + "' and c.queryDate <='" + str1 + "'";

		}
		jpql += " order by c.queryDate desc";
		Query query = entityManager.createQuery(jpql);
		Query query1 = entityManager.createQuery(jpql);

		query.setFirstResult((page - 1) * pageSize);
		query.setMaxResults(pageSize);
		List<PatentQueryRecordRo> patentQueryRecordRos = query.getResultList();
		Map<String, Object> map = new HashMap<>();
		map.put("totle", query1.getResultList().size());
		map.put("result", patentQueryRecordRos);
		ResidueCount residueCount = repository.getPatentSearch();
		ResidueCount residueInfoCount = repository.getPatentSearchInfo();
		int temp = 0;
		map.put("count", (temp = residueCount.getResidueCount()) >= 0 ? temp : 0);
		map.put("lastTime", residueCount.getLastQueryDate());
		temp = 0;
		map.put("countInfo", (temp = residueInfoCount.getResidueCount()) >= 0 ? temp : 0);
		map.put("lastInfoTime", residueInfoCount.getLastQueryDate());
		return map;
	}

}
